package cn.jianyongjie.basic.service.impl;

import cn.jianyongjie.basic.mapper.BaseMapper;
import cn.jianyongjie.basic.query.BaseQuery;
import cn.jianyongjie.basic.service.IBaseService;
import cn.jianyongjie.basic.utils.QueryResult;
import cn.jianyongjie.org.domain.Department;
import org.hibernate.validator.internal.util.stereotypes.ThreadSafe;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* @Description: TODO
* @Author: JianYongJie
* @Version: 1.0
* @Date:  2022/4/25 17:44
*/
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {

    @Autowired
    private BaseMapper<T> baseMapper;

    /*
    * @Title: BaseServiceimpl
    * @Description: 分页查询
    * @Author: JianYongJie
    * @Version: 1.0
    * @Date:  2022/4/25 17:50
    * @Parameters: * @param baseQuery
    * @Return cn.jianyongjie.basic.utils.QueryResult<T>
    */
    @Override
    public QueryResult<T> queryData(BaseQuery baseQuery) {
        // 查询总条数
        Long total = baseMapper.queryCount(baseQuery);
        // 查询分页数据
        List<T> rows = baseMapper.queryData(baseQuery);
        // 封装到QueryResult工具类里面
        return new QueryResult<T>(total,rows);
    }

    /*
    * @Title: BaseServiceimpl
    * @Description: 查询总条数
    * @Author: JianYongJie
    * @Version: 1.0
    * @Date:  2022/4/25 17:50
    * @Parameters: * @param baseQuery
    * @Return java.lang.Integer
    */
    @Override
        public Long queryCount(BaseQuery baseQuery) {
        return baseMapper.queryCount(baseQuery);
    }

    /*
    * @Title: BaseServiceimpl
    * @Description: 根据id查询
    * @Author: JianYongJie
    * @Version: 1.0
    * @Date:  2022/4/25 17:51
    * @Parameters: * @param id
    * @Return T
    */
    @Override
    public T loadById(Long id) {
        return baseMapper.LoadById(id);
    }

    /*
    * @Title: BaseServiceimpl
    * @Description: 根据id删除
    * @Author: JianYongJie
    * @Version: 1.0
    * @Date:  2022/4/25 17:52
    * @Parameters: * @param id
    * @Return void
    */
    @Override
    @Transactional
    public void remove(Long id) {
        baseMapper.remove(id);
    }

    /*
    * @Title: BaseServiceimpl
    * @Description: 新增
    * @Author: JianYongJie
    * @Version: 1.0
    * @Date:  2022/4/25 17:52
    * @Parameters: * @param t
    * @Return void
    */
    @Override
    @Transactional
    public void save(T t) {
        baseMapper.save(t);
    }


    /*
    * @Title: BaseServiceimpl
    * @Description: 修改
    * @Author: JianYongJie
    * @Version: 1.0
    * @Date:  2022/4/25 17:53
    * @Parameters: * @param t
    * @Return void
    */
    @Override
    @Transactional
    public void update(T t) {
        baseMapper.update(t);
    }

    /*
    * @Title: BaseServiceimpl
    * @Description: 批量删除
    * @Author: JianYongJie
    * @Version: 1.0
    * @Date:  2022/4/25 17:53
    * @Parameters: * @param ids
    * @Return void
    */
    @Override
    @Transactional
    public void batchRemove(List<Long> ids) {
        baseMapper.batchRemove(ids);
    }

    /*
    * @Title: BaseServiceimpl
    * @Description: 查询所有数据
    * @Author: JianYongJie
    * @Version: 1.0
    * @Date:  2022/4/25 17:53
    * @Parameters: * @param
    * @Return java.util.List<T>
    */
    @Override
    public List<T> loadAll() {
        return baseMapper.loadAll();
    }
}
